<template>
  <a-layout>
    <a-layout-content
        :style="{ background: '#fff', padding: '24px', margin: 0, minHeight: '280px' }"
    >
      <a-space size="small">
        <a-button type="primary" @click="add" >
          新增
        </a-button>
      </a-space>
      <a-table :columns="columns"
               :row-key="record => record.id"
               :data-source="level1"
               :loading="loading"
               :pagination="false">
        <template v-slot:action="{ text, record}">
          <a-space size="small">
            <a-button type="primary" @click="edit(record)">
              编辑
            </a-button>
            <a-popconfirm
                title="该删除操作不可逆-确定删除吗?"
                ok-text="确定"
                cancel-text="取消"
                @confirm="handleDelete(record.id)"
            >
              <a-button type="primary" danger>
                删除
              </a-button>
            </a-popconfirm>
          </a-space>
        </template>
      </a-table>
    </a-layout-content>
  </a-layout>
  <a-modal title="分类表单"
           v-model:visible="modalVisible"
           :confirm-loading="modalLoading"
           @ok="handleModalOk">
    <a-form :model="category" :label-col="{ span : 6}" :wrapper-col="wrapperCol">
      <a-form-item label="名称">
        <a-input v-model:value="category.name"/>
      </a-form-item>
      <a-form-item label="父分类">
        <a-select
            ref="select"
            v-model:value="category.parent"
        >
          <a-select-option value="0">无</a-select-option>
          <a-select-option v-for="c in level1" :key="c.id" :value="c.id" :disabled="c.id===category.id">
            {{ c.name }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="排序权重">
        <a-input v-model:value="category.sort" type="text"/>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script lang="ts">
import {defineComponent, onMounted, ref} from 'vue';
import axios from "axios";
import {message} from 'ant-design-vue'
import {Tool} from "@/util/tool";

export default defineComponent({
  name: 'AdminCategory',
  setup() {
    // 分类信息响应式对象
    const categorys = ref();
    // 表示加载状态
    const loading = ref(false);
    // 表格列信息
    const columns = [
      {
        title: '名称',
        dataIndex: 'name',
      },
      {
        title: '父分类',
        key: 'parent',
        dataIndex: 'parent',
      },
      {
        title: '排序权重',
        dataIndex: 'sort',
      },
      {
        title: 'Action',
        key: 'action',
        slots: {customRender: 'action'},
      },
    ];

    const level1 = ref();
    /**
     * 数据查询
     */
    const handleQuery = () => {
      loading.value = true;
      axios.get("/category/list").then((response) => {
        loading.value = false;
        const data = response.data;
        if (data.success) {
          level1.value = [];
          level1.value = Tool.array2Tree(data.content,0);
        } else {
          message.error(data.message);
        }
      })
    };


    // 表单
    const category = ref();
    const modalVisible = ref(false);
    const modalLoading = ref(false);
    const handleModalOk = () => {
      modalLoading.value = true;
      // 编辑保存分类
      axios.post("/category/save", category.value).then((response) => {
        modalLoading.value = true;
        loading.value = false;
        const data = response.data;
        if (data.success) {
          modalVisible.value = false;
          modalLoading.value = false;
          message.success("操作成功")
          handleQuery();
        } else {
          modalLoading.value = false;
          message.error(data.message)
        }
      })
    };

    // 编辑分类方法
    const edit = (record: any) => {
      modalVisible.value = true;
      category.value = Tool.copy(record);
    }

    // 新增分类方法
    const add = () => {
      modalVisible.value = true;
      category.value = {};
    }

    // 删除分类方法
    const handleDelete = (id: number) => {
      axios.delete("/category/delete/" + id).then((response) => {
        const data = response.data;
        if (data.success) {
          message.success("操作成功")
          handleQuery();
        }
      })
    }

    onMounted(() => {
      handleQuery();
    });
    return {
      categorys,
      category,
      loading,
      columns,
      modalVisible,
      modalLoading,
      edit,
      add,
      handleModalOk,
      handleDelete,
      level1
    }
  }
});

</script>
